import { createRouter, createWebHistory } from 'vue-router'
import Layout from '@/layout/index.vue'

export const routes = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/login/index.vue'),
    hidden: true
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: () => import('@/views/dashboard/index.vue'),
        meta: { title: '首页', icon: 'House' }
      }
    ]
  },
  {
    path: '/member',
    component: Layout,
    name: 'Member',
    meta: { title: '会员管理', icon: 'User' },
    children: [
      {
        path: 'list',
        name: 'MemberList',
        component: () => import('@/views/member/List.vue'),
        meta: { title: '会员列表' }
      },
      {
        path: 'organization',
        name: 'OrganizationList',
        component: () => import('@/views/organization/index.vue'),
        meta: { title: '企业机构管理' }
      }
    ]
  },
  {
    path: '/black-card',
    component: Layout,
    name: 'BlackCard',
    meta: { title: '黑卡管理', icon: 'CreditCard' },
    children: [
      {
        path: 'list',
        name: 'BlackCardList',
        component: () => import('@/views/black-card/List.vue'),
        meta: { title: '黑卡列表' }
      },
      {
        path: 'sub-card',
        name: 'SubCardList',
        component: () => import('@/views/black-card/sub-card.vue'),
        meta: { title: '子卡管理' }
      },
      {
        path: 'rule',
        name: 'CardRule',
        component: () => import('@/views/black-card/rule.vue'),
        meta: { title: '卡号规则' }
      },
      {
        path: 'recharge/list',
        name: 'RechargeList',
        component: () => import('@/views/black-card/RechargeList.vue'),
        meta: { title: '充值记录' }
      },
      {
        path: 'recharge/form',
        name: 'RechargeForm',
        component: () => import('@/views/black-card/RechargeForm.vue'),
        meta: { title: '黑卡充值', activeMenu: '/black-card/recharge/list' },
        hidden: true
      },
      {
        path: 'recharge/detail',
        name: 'RechargeDetail',
        component: () => import('@/views/black-card/RechargeDetail.vue'),
        meta: { title: '充值详情', activeMenu: '/black-card/recharge/list' },
        hidden: true
      },
      {
        path: 'quota',
        name: 'CardQuota',
        component: () => import('@/views/black-card/QuotaList.vue'),
        meta: { title: '子卡余额分配' }
      },
      {
        path: 'balance',
        name: 'CardBalance',
        component: () => import('@/views/black-card/BalanceLog.vue'),
        meta: { title: '余额变更记录' }
      }
    ]
  },
  {
    path: '/product',
    component: Layout,
    name: 'Product',
    meta: { title: '产品管理', icon: 'Goods' },
    children: [
      {
        path: 'category',
        name: 'ProductCategory',
        component: () => import('@/views/product/category/index.vue'),
        meta: { title: '产品分类', icon: 'Menu' }
      },
      {
        path: 'scenic',
        name: 'ScenicProduct',
        component: () => import('@/views/product/scenic/index.vue'),
        meta: { title: '景点产品', icon: 'Picture' }
      }
    ]
  },
  {
    path: '/order',
    component: Layout,
    name: 'Order',
    meta: { title: '订单管理', icon: 'Tickets' },
    children: [
      {
        path: 'recharge',
        name: 'OrderRecharge',
        component: () => import('@/views/order/recharge.vue'),
        meta: { title: '充值订单', icon: 'Money' }
      },
      {
        path: 'consume',
        name: 'ConsumeOrder',
        component: () => import('@/views/order/consume.vue'),
        meta: { title: '消费订单', icon: 'ShoppingCart' }
      }
    ]
  },
  {
    path: '/business',
    component: Layout,
    redirect: '/business/partner',
    name: 'Business',
    meta: {
      title: '商家管理',
      icon: 'Shop'
    },
    children: [
      {
        path: 'type',
        name: 'BusinessType',
        component: () => import('@/views/business/type/index.vue'),
        meta: { title: '商家类型' }
      },
      {
        path: 'partner',
        name: 'BusinessPartner',
        component: () => import('@/views/business/partner/index.vue'),
        meta: { title: '商家列表' }
      },
      {
        path: 'employee',
        name: 'BusinessEmployee',
        component: () => import('@/views/business/employee/index.vue'),
        meta: { title: '员工管理', icon: 'User' }
      }
    ]
  },
  {
    path: '/member-rights',
    component: Layout,
    name: 'MemberRights',
    meta: { title: '会员权益管理', icon: 'Present' },
    children: [
      {
        path: 'list',
        name: 'RightsList',
        component: () => import('@/views/member-rights/list.vue'),
        meta: { title: '权益列表' }
      },
      {
        path: 'rules',
        name: 'RightsRules',
        component: () => import('@/views/member-rights/rules.vue'),
        meta: { title: '权益规则' }
      }
    ]
  },
  {
    path: '/service',
    component: Layout,
    name: 'Service',
    meta: { title: '服务管家管理', icon: 'Service' },
    children: [
      {
        path: 'list',
        name: 'ServiceList',
        component: () => import('@/views/service/list.vue'),
        meta: { title: '服务管家列表' }
      },
      {
        path: 'assign',
        name: 'ServiceAssign',
        component: () => import('@/views/service/assign.vue'),
        meta: { title: '服务管家分配' }
      },
      {
        path: 'record',
        name: 'ServiceRecord',
        component: () => import('@/views/service/record.vue'),
        meta: { title: '服务记录' }
      }
    ]
  }
]

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  // 获取token
  const token = localStorage.getItem('token')
  
  // 如果访问登录页，直接放行
  if (to.path === '/login') {
    if (token) {
      next('/')
    } else {
      next()
    }
    return
  }
  
  // 如果没有token，跳转到登录页
  if (!token) {
    next('/login')
    return
  }
  
  next()
})

export default router 